#include "engine.h"
#include "stringhelper.h"

namespace stringhelper
{
	bool validate(const string& filename)
	{
		if (string::npos!=filename.find('\\')) return false;
		for (uint32 i=0;i<filename.size();i++) if (filename[i]>='A' && filename[i]<='Z') return false;
		return true;
	}

	string get_extension(const string& filename)
	{
		string extension;
		size_t dotpos=filename.rfind('.');
		if (string::npos!=dotpos) extension=filename.substr(dotpos+1);
		return extension;
	}

	string get_path(const string& filename)
	{
		string newpath;
		size_t slashpos=filename.rfind('/');
		if (string::npos!=slashpos) newpath=filename.substr(0, slashpos+1);
		return newpath;
	}

	string get_filename(const string& filename)
	{
		string newfilename;
		size_t slashpos=filename.rfind('/');
		if (string::npos==slashpos)
		{
			newfilename=filename;
		} else {
			newfilename=filename.substr(slashpos+1, filename.size());
		}
		return newfilename;
	}

	string remove_extension(const string& filename)
	{
		string noextension;
		size_t dotpos=filename.rfind('.');
		if (string::npos==dotpos)
			noextension=filename;
		else
			noextension=filename.substr(0, dotpos);
		return noextension;
	}

	string to_lower(const string& filename)
	{
		string lowercase=filename;
		for (uint32 i=0;i<lowercase.length();i++)
			if (lowercase[i]>='A' && lowercase[i]<='Z') lowercase[i]-='A'-'a';
		return lowercase;
	}

	string fixup(const string& filename)
	{
		string r=to_lower(filename);
		for (uint32 i=0;i<r.length();i++) if (r[i]=='\\') r[i]='/';
		return r;
	}
}